package com.hurl.waguji.biz.collect;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.hurl.waguji.base.SqlReader;
import com.hurl.waguji.base.dao.HibernateDao;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjCwLrbHexunBO;
import com.hurl.waguji.persistence.JcsjCwZcfzbHexunBO;
import com.hurl.waguji.persistence.SjProfileCwBO;

public class ProfileCwHandler extends BaseCollectHandler {
	private static final Logger log = Logger.getLogger(ProfileCwHandler.class);
	
	@Override
	public void startup(HibernateDao dao) {

	}

	@Override
	public void execute(DmGpBO gp, HibernateDao dao) {
		SjProfileCwBO cw = new SjProfileCwBO();

		cw.setGpdm(gp.getGpDm());

		fillZcfzb(cw, gp, dao);
		fillLrb(cw, gp, dao);
		
		saveProfile(cw,dao);
	}

	private boolean isExists(SjProfileCwBO profile,HibernateDao dao){
		return dao.queryBySQL(SqlReader.getSql("SQL12041901", new String[]{profile.getGpdm()})).setReadOnly(true).list().size()>0;
	}
	private void saveProfile(SjProfileCwBO cw, HibernateDao dao){
		if(isExists(cw,dao)){
			dao.update(cw);
		}else{
			dao.save(cw);
		}
	}
	
	private void fillLrb(SjProfileCwBO cw, DmGpBO gp, HibernateDao dao) {
		List<JcsjCwLrbHexunBO> list = (List<JcsjCwLrbHexunBO>) dao
				.createCriteria(JcsjCwLrbHexunBO.class)
				.add(Restrictions.eq("id.gpdm", gp.getGpDm()))
				.addOrder(Order.desc("id.rq")).setFetchSize(1).setReadOnly(true).list();
		if (list.isEmpty()) {
			return;
		}
		
		JcsjCwLrbHexunBO bo = list.get(0);
		
		cw.setLrbBtsr(bo.getBtsr());
		cw.setLrbGlfy(bo.getGlfy());
		cw.setLrbGsmgssyzjlr(bo.getGsmgssyzjlr());
		cw.setLrbJbmgsy(bo.getJbmgsy());
		cw.setLrbJlr(bo.getJlr());
		cw.setLrbLrze(bo.getLrze());
		cw.setLrbMgsy(bo.getMgsy());
		cw.setLrbRq(bo.getId().getRq());
		cw.setLrbSds(bo.getSds());
		cw.setLrbSsgdsy(bo.getSsgdsy());
		cw.setLrbTzsy(bo.getTzsy());
		cw.setLrbXsfy(bo.getXsfy());
		cw.setLrbXsmgsy(bo.getXsmgsy());
		cw.setLrbYycb(bo.getYycb());
		cw.setLrbYylr(bo.getYylr());
		cw.setLrbYysjjfj(bo.getYysjjfj());
		cw.setLrbYysr(bo.getYysr());
		cw.setLrbYywsr(bo.getYywsr());
		cw.setLrbYywzc(bo.getYywzc());
	}

	private void fillZcfzb(SjProfileCwBO cw, DmGpBO gp, HibernateDao dao) {
		List<JcsjCwZcfzbHexunBO> list = (List<JcsjCwZcfzbHexunBO>) dao
				.createCriteria(JcsjCwZcfzbHexunBO.class)
				.add(Restrictions.eq("id.gpdm", gp.getGpDm()))
				.addOrder(Order.desc("id.rq")).setFetchSize(1).setReadOnly(true).list();
		if (list.isEmpty()) {
			return;
		}

		JcsjCwZcfzbHexunBO bo = list.get(0);

		cw.setZcfzbCh(bo.getCh());
		cw.setZcfzbCqjk(bo.getCqjk());
		cw.setZcfzbFldfzhj(bo.getFldfzhj());
		cw.setZcfzbFldzchj(bo.getFldzchj());
		cw.setZcfzbFzhj(bo.getFzhj());
		cw.setZcfzbFzsyzqyhj(bo.getFzsyzqyhj());
		cw.setZcfzbGdzc(bo.getGdzc());
		cw.setZcfzbGsmgssyzqy(bo.getGsmgssyzqy());
		cw.setZcfzbLdfzhj(bo.getLdfzhj());
		cw.setZcfzbLdzchj(bo.getLdzchj());
		cw.setZcfzbRq(bo.getId().getRq());
		cw.setZcfzbSszb(bo.getSszb());
		cw.setZcfzbSyzqyhj(bo.getSyzqyhj());
		cw.setZcfzbWxzc(bo.getWxzc());
		cw.setZcfzbYnndqldfz(bo.getYnndqldfz());
		cw.setZcfzbYygj(bo.getYygj());
		cw.setZcfzbZbgj(bo.getZbgj());
		cw.setZcfzbZczj(bo.getZczj());
	}

	@Override
	public void shutdown(HibernateDao dao) {

	}

}
